package controller;

import java.util.List;

import model.NguoiDung;

import org.hibernate.WrongClassException;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import dao.entity.NguoiDungDAO;

public class Login extends SelectorComposer<Component> {
	private static final long serialVersionUID = 1L;
	@Wire
	private Textbox email;
	@Wire
	private Textbox password;
	@Wire
	private Label saiemail;

	@Listen("onClick=#submitButton")
	public void login() {
		NguoiDung nguoiDung = new NguoiDung(email.getValue(),
				SHA1.getSHA1(password.getValue()));
		System.out.println(SHA1.getSHA1(password.getValue()));
		NguoiDungDAO dao = new NguoiDungDAO();
		int capDo = dao
				.findByCriteria(Restrictions.eq("email", email.getValue()))
				.get(0).getCapDo().getSoCapDo();
		boolean isExist = NguoiDungDAO.checkLogin(nguoiDung);
		if (!isExist) {
			throw new WrongValueException(email,
					"Email hoặc Password không đúng");
		} else {

			Sessions.getCurrent().setAttribute("email", email.getValue());
			Executions.sendRedirect("");
		}
	}

}
